package 删除链表的倒数第N个结点;

/**
 * @author zhengTao
 * @version 1.0
 * @description: 删除链表的倒数第 N 个结点
 * @date 2022/10/23 10:45
 */
public class Demo19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode pre=new ListNode(-1);
        pre.next=head;
        ListNode fast=pre;
        ListNode slow=pre;
        while (n>0){
            fast=fast.next;
            n--;
        }
        while (fast.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        slow.next=slow.next.next;
        return pre.next;
    }
    public class ListNode {
        int val;
       ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
}
